package com.jlect.swebing.renderers.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.jlect.swebing.renderers.client.ContainerRenderer;
import com.jlect.swebing.renderers.client.Renderer;

/**
 * Container renderer GWT based implementation
 * @author Sergey Kozmin
 * @since 11.11.2007 21:02:27
 */
public class ContainerRendererGWTImpl extends AbstractGWTRenderer implements ContainerRenderer {
    public void addElement(Renderer renderer) {
        getPanel().add(checkIfAcceptable(renderer).getWidget());
    }

    public void removeElement(Renderer renderer) {
        getPanel().remove(checkIfAcceptable(renderer).getWidget());
    }

    private WidgetProvider checkIfAcceptable(Renderer proving) {
        if(!(proving instanceof WidgetProvider)) {
            throw new IllegalArgumentException("Could not work with components that is not a [WidgetProvider] class," +
                    " the given component has class [" + GWT.getTypeName(proving) + "]. ");
        } else {
            return (WidgetProvider) proving;
        }
    }

    protected Panel getPanel() {
        return (Panel) ((CompositeAttachEventProvider)getWidget()).getTargetWidget();
    }

    protected Widget provideWidget() {
        return new CompositeAttachEventProvider(new AbsolutePanel());
    }
}
